﻿@model TGIApp.Model.Address
<script type="text/javascript">

    $(function () {

        $("#cityName").autocomplete({
            source: function (request, response) {
                $.ajax({
                //Calling FindCities
                    url: "/home/findcities", type: "POST", dataType: "json",
                    data: { searchText: request.term, maxResults: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                        //gettting Id, CityName and State
                            return { label: item.CityName, value: item.CityName, id: item.Id, state: item.State }
                        }))
                    }
                })
            },
            select: function (event, ui) {
                if (ui.item) {
                    //Setting State
                    $("#stateName").get()[0].value = ui.item.state;
                    //Setting CityId
                    $("#hiddenCityId").get()[0].value = ui.item.id
                }
                
            }
        });

    });
</script>
<div class="editor-label">
    @Html.LabelFor(model => model.Line1)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Line1)
    @Html.ValidationMessageFor(model => model.Line1)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Line2)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Line2)
    @Html.ValidationMessageFor(model => model.Line2)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.City)
</div>
        <!-- Put a hidden field to keep track of city_id otherwise when posting the form it will take User.Address.City.Id = 0 when object is created for Edit (post) -->
      @*  @{ if (ViewContext.RouteData.Values["Action"].ToString().ToLower() == "edit")
           { *@
            <div>
                @Html.HiddenFor(model => model.City.Id, new { id="hiddenCityId"})
            </div>
        @*   }
        }*@
<div>
    @Html.TextBoxFor(model => model.City.CityName, new { id = "cityName" })
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.City.State)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.City.State, new { readOnly = "true", id = "stateName" })
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.PostalCode)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.PostalCode)
    @Html.ValidationMessageFor(model => model.PostalCode)
</div>
